<template>
	<view class="all-main">
		<u-loading-page :loading="loadding" loading-mode="semicircle" loading-text="欢迎进入风动社区"></u-loading-page>
		<view v-if="!loadding" class="main">
			<view class="con">
				<radio-group @change="handleChange">
					<view v-for="(item, index) in proList" :key="index" class="items">
						<view class="lists">
							<view class="lefts">
								<view class="image">
									<u--image :src="item.image" width="50" height="50" shape="square" radius="10"></u--image>
								</view>
								<view class="title">
									<view class="titles">
										{{item.name}}
									</view>
									<view class="nums">
										<text>库存：</text>
										<text class="tit">{{item.stock}}</text>
									</view>
								</view>
							</view>
							<view class="rights">
								<label class="radio">
									<radio :value="index+''" />
								</label>
							</view>
						</view>
						<u-line></u-line>
					</view>
				</radio-group>
			</view>
			<view class="num_items">
				<view class="lefts">
					数量
				</view>
				<view class="rights">
					<u-number-box v-model="number" :step="1" :max="product_num" :min="0"></u-number-box>
				</view>
			</view>
			<view class="btn" @click="saveData">
				申请
			</view>
		</view>
	</view>
</template>

<script>
	var util = require('@/util/request5.js');
	export default {
		data() {
			return {
				loadding: true,
				product_id: '',
				product_name: '',
				product_num: 0,
				proList: [],
				number: 0,
				customerinfo: {},
				is_true: true
			}
		},
		onLoad() {
			this.customerinfo = uni.getStorageSync('customerinfo');
			this.getProduct()
		},
		methods: {
			// 获取产品
			async getProduct() {
				const {
					data: res
				} = await this.$myHttp5({
					url: 'v1/getGiftList',
					data: {
						type: 5
					},
					header: {
						'token': this.customerinfo.token
					}
				});
				this.loadding = false
				if (res.code == 1) {
					res.data.map(item => {
						item.image = util.API_URL + item.image
					})
					this.proList = res.data
				}
			},
			// 选择产品
			handleChange(e) {
				this.product_id = this.proList[e.detail.value].id,
				this.product_num = this.proList[e.detail.value].stock
			},
			// 保存信息
			async saveData() {
				if (this.product_id == '') {
					uni.showToast({
						title: '请选择礼品',
						icon: "none"
					});
					return false
				}
				if (this.number <= 0) {
					uni.showToast({
						title: '请输入数量',
						icon: "none"
					});
					return false
				}
				var that = this
				if (this.is_true) {
					this.is_true = false
					uni.showLoading({
						title: '加载中'
					});
					const {
						data: res
					} = await this.$myHttp5({
						url: 'gift/saveApply',
						data: {
							gift_id: this.product_id,
							number: this.number
						},
						header: {
							'token': this.customerinfo.token
						}
					});
					this.is_true = true
					uni.hideLoading()
					uni.showToast({
						title: '提交成功',
						icon: "none"
					});
					if (res.code == 1) {
						setTimeout(function() {
							uni.navigateBack({
								delta: 1, // 返回的页面数，如果为1表示返回上一页
							});
						},1500)
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #f3f4f6;
	}
	
	.all-main {
		.main {
			padding: 20rpx;
			.con {
				background-color: #ffffff;
				.items {
					.lists {
						padding: 30rpx;
						display: flex;
						justify-content: space-between;
						.lefts {
							width: 80%;
							display: flex;
							justify-content: flex-start;
							align-items: center;
							.title {
								font-size: 32rpx;
								padding-left: 20rpx;
								padding-bottom: 10rpx;
								.nums {
									padding-top: 10rpx;
									font-size: 28rpx;
									.tit {
										font-weight: bold;
										color: #fa3534;
										font-size: 32rpx;
									}
								}
							}
						}
						.rights {
							display: flex;
							justify-content: flex-end;
							align-items: center;
							width: 20%;
						}
					}
				}
			}
			.btn {
				position: fixed;
				left: 0;
				right: 0;
				bottom: 0;
				margin-top: 50rpx;
				background-color: #2979ff;
				color: #ffffff;
				font-weight: bold;
				text-align: center;
				padding: 30rpx;
			}
			.num_items {
				margin: 20rpx 0;
				background-color: #ffffff;
				padding: 30rpx;
				display: flex;
				justify-content: space-between;
				.lefts {
					width: 20%;
				}
				.rights {
					display: flex;
					justify-content: flex-end;
					align-items: center;
					width: 80%;
					input {
						width: 100%;
						text-align: right;
					}
				}
			}
		}
	}
</style>
